package com.gilgamesh.mall.product.sql;

import com.gilgamesh.mall.sdk.bean.product.ProductEntity;
import com.gilgamesh.mall.sdk.util.BeanUtil;
import com.gilgamesh.mall.sdk.util.StringUtil;

/**
 * Created by Peiyao.Wang on 2021/1/12
 *
 * @author Peiyao.Wang
 * @date 2021/1/12
 */
public class ProductSql {
    public String listProducts(ProductEntity productEntity){
        StringBuilder sql = new StringBuilder();
        sql.append("select * from t_product");
        if(BeanUtil.isLogicalNull(productEntity)){
            return sql.toString();
        }
        int times = 0;
        if(productEntity.getId() != null) {
            if (times == 0) {
                sql.append(" where ");
            }else if(times > 0){
                sql.append(" and ");
            }
            times++;
            sql.append("id = #{id}");
        }
        if(StringUtil.isNotEmpty(productEntity.getProductCode())){
            if(times == 0){
                sql.append(" where ");
            }else if(times > 0){
                sql.append(" and ");
            }
            times++;
            sql.append(" product_code = #{productCode}");
        }
        if(productEntity.getClassfic3Id() != null){
            if(times == 0){
                sql.append(" where ");
            }else if(times > 0){
                sql.append(" and ");
            }
            times++;
            sql.append(" classfic3_id = #{classfic3Id}");
        }
        if(productEntity.getClassfic2Id() != null){
            if(times == 0){
                sql.append(" where ");
            }else if(times > 0){
                sql.append("and");
            }
            times++;
            sql.append(" classfic2_id = #{classfic2Id} ");
        }
        if(productEntity.getClassficId() != null){
            if(times == 0){
                sql.append(" where ");
            }else if(times > 0){
                sql.append(" and ");
            }
            times++;
            sql.append(" classfic_id = #{classficId} ");
        }

        if(StringUtil.isNotEmpty(productEntity.getShopCode())){
            if(times == 0){
                sql.append(" where ");
            }else if(times > 0){
                sql.append(" and ");
            }
            times++;
            sql.append(" shop_code = #{shopCode} ");
        }
        if(productEntity.getCreateTime() != null){
            if(times == 0){
                sql.append(" where ");
            }else if(times > 0){
                sql.append(" and ");
            }
            sql.append(" create_time = #{createTime} ");
        }
        return sql.toString();

    }
}
